package com.hschinese.pronunciation.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.hschinese.pronunciation.bean.AnonymityBean;
import com.hschinese.pronunciation.bean.UserInfo;

public class SharedPreferenceUtils {

	private static SharedPreferenceUtils getshared = null;
	private static SharedPreferences settings;
	public static final String IS_FIRST_LOGIN = "isFirstLogin"; // 是否是第一次登录
	public static final String IS_LOGINED = "is_logined"; // 是否已经登录
	private static final String Email = "Email";
	private static final String Balance = "Balance";
	private static final String Uid = "Uid";
	private static final String Nickname = "Nickname";
	private static final String Role = "Role";
	private static final String EndTime = "EndTime";
	private static final String Name = "Name";
	private static final String EndDate = "EndDate";
	private static final String Avatars = "Avatars";
	private static final String Enabled = "Enabled";
	private static final String Password = "Password";
	private static final String Anonymity = "Anonymity";
	private static final String Nemail = "Nemail";

	private SharedPreferenceUtils(Context ctx) {
		settings = ctx.getSharedPreferences(Constant.SP_BASIC, 0);
	}
	
	public static SharedPreferenceUtils getInstance(Context ctx) {
		synchronized (SharedPreferenceUtils.class) {
			if (null == getshared) {
				getshared = new SharedPreferenceUtils(ctx);
			}
			return getshared;
		}
	}

	public void setFirstLoginFlag() {
		Editor editor = settings.edit();
		editor.putBoolean(IS_FIRST_LOGIN, false);
		editor.commit();
	}

	public boolean getFirstLoginFlag() {
		return settings.getBoolean(IS_FIRST_LOGIN, true);
	}
	
	public String getSharedKey(String string,String defaultStr){
		return settings.getString(string, defaultStr);
	}
	
	public void setSharedKey(String key,int value){
		Editor editor = settings.edit();
		editor.putInt(key, value);
		editor.commit();
	}
	
	public int getSharedKeyInt(String string){
		return settings.getInt(string, 0);
	}
	
	public boolean getSharedKeyB(String string){
		return settings.getBoolean(string, false);
	}
	
	public void setSharedKey(String key,boolean value){
		Editor editor = settings.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	public void setIsLogined(boolean b) {
		Editor editor = settings.edit();
		editor.putBoolean(IS_LOGINED, b);
		editor.commit();
	}

	public boolean getIsLogined() {
		return settings.getBoolean(IS_LOGINED, false);
	}

	public void setUserInfo(UserInfo info) {
		Editor editor = settings.edit();
		editor.putString(Email, info.getEmail());
		editor.putString(Balance, info.getBalance());
		editor.putString(Uid, info.getUid());
		editor.putString(Nickname, info.getNickname());
		editor.putString(Role, info.getRole());
		editor.putString(EndTime, info.getEndTime());
		editor.putString(Name, info.getName());
		editor.putString(EndDate, info.getEndDate());
		editor.putString(Avatars, info.getAvatars());
		editor.putBoolean(Anonymity, info.getAnonymity());
		editor.putString(Nemail, info.getNemail());
		editor.commit();
	}

	public UserInfo getuserInfo() {
		UserInfo info = new UserInfo();
		info.setEmail(settings.getString(Email, ""));
		info.setBalance(settings.getString(Balance, ""));
		info.setUid(settings.getString(Uid, ""));
		info.setNickname(settings.getString(Nickname, ""));
		info.setRole(settings.getString(Role, ""));
		info.setEndTime(settings.getString(EndTime, ""));
		info.setName(settings.getString(Name, ""));
		info.setEndDate(settings.getString(EndDate, ""));
		info.setAvatars(settings.getString(Avatars, ""));
		info.setAnonymity(settings.getBoolean(Anonymity, false));
		info.setNemail(settings.getString(Nemail, ""));
		return info;
	}

	public void setAnonymityInfo(AnonymityBean info) {
		Editor editor = settings.edit();
		editor.putString(Email, info.getEmail());
		editor.putString(Uid, info.getUid());
		editor.putString(Nickname, info.getNickname());
		editor.putBoolean(Enabled, info.isEnabled());
		editor.putString(Password, info.getPassword());
		editor.putString(Avatars, info.getAvatars());
		editor.commit();
	}

	public AnonymityBean getAnonmityInfo() {
		AnonymityBean info = new AnonymityBean();
		info.setEmail(settings.getString(Email, ""));
		info.setUid(settings.getString(Uid, ""));
		info.setNickname(settings.getString(Nickname, ""));
		info.setEnabled(settings.getBoolean(Enabled, false));
		info.setPassword(settings.getString(Password, ""));
		info.setAvatars(settings.getString(Avatars, ""));
		return info;
	}
}
